package javamobile.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.mybeans.dao.DAOException;

import javamobile.model.*;

public class FriendsListAction extends Action{

	private DAOFactory factory;
	
	public FriendsListAction(Model model) {
		factory = model.getDAOFactory();
	}
	
	@Override
	public String getName() {return "friends.do";}

	@Override
	public Map<String, Object> perform(Map<String, Object> input) {
		System.out.println("Performing friend action!");
		Map<String, Object> output = new HashMap<String, Object>();
		List<String> errors = new ArrayList<String>();
		output.put("errors", errors);
		
		String username = (String) input.get("username");

		try {
			User user = factory.lookupUser(username);
			if (user == null) {
            errors.add("User "+username+" not found");
            return output;
			}

            Map<String, String> friendList = factory.getFriendsList(user.getUserID());
            Map<String, String> pendingList = factory.getPendingList(user.getUserID());
            output.put("friend", friendList);
            output.put("pending", pendingList);
        }
		catch (DAOException e) {
        	errors.add(e.getMessage());
        	return output;
	    }

		return output;
	}

}